package com.carrot.api.controller.system;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.carrot.common.core.controller.BaseController;
import com.carrot.common.core.domain.AjaxResult;
import com.carrot.common.core.domain.dto.StationCellTypeDto;
import com.carrot.common.core.domain.entity.SysStationCellType;
import com.carrot.common.core.domain.vo.StationCellTypeVo;
import com.carrot.common.enums.CellTypeEnum;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.utils.WrapperUtil;
import com.carrot.system.service.ISysStationCellTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 场站单体点表
 *
 * @author liuhongzhen
 */
@RestController
@RequestMapping("/system/station-cell-type")
public class SysStationCellTypeController extends BaseController {

    @Autowired
    private ISysStationCellTypeService stationCellTypeService;

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("detail")
    public AjaxResult detail(@RequestParam(value = "id", required = false) Long id) {
        SysStationCellType cellType =
                stationCellTypeService.query()
                        .eq("id", id)
                        .one();
        StationCellTypeVo cellTypeVo = WrapperUtil.transform(cellType, StationCellTypeVo.class);
        cellTypeVo.setCellTypeName(CellTypeEnum.getCellTypeName(cellTypeVo.getCellType()));

        return AjaxResult.success(cellTypeVo);
    }

    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "stationId", required = false) String stationId) {

        List<SysStationCellType> list =
                stationCellTypeService.query()
                        .eq("station_id", stationId)
                        .orderByAsc("id")
                        .list();
        List<StationCellTypeVo> voList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(list)) {
            for (SysStationCellType cellType : list) {
                StationCellTypeVo cellTypeVo = WrapperUtil.transform(cellType, StationCellTypeVo.class);
                cellTypeVo.setCellTypeName(CellTypeEnum.getCellTypeName(cellTypeVo.getCellType()));

                voList.add(cellTypeVo);
            }
        }
        return AjaxResult.success(voList);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody StationCellTypeDto dto) {
        stationCellTypeService.add(dto);
        return AjaxResult.success("添加成功");
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody StationCellTypeDto dto) {
        stationCellTypeService.update(dto);
        return AjaxResult.success("更新成功");
    }

    @GetMapping("/del")
    public AjaxResult del(@RequestParam("id") Long id) {
        stationCellTypeService.del(id);
        return AjaxResult.success("删除成功");
    }

    @PostMapping("/batchDel")
    public AjaxResult batchDel(@RequestBody StationCellTypeDto dto) {
        if (CollUtil.isEmpty(dto.getIds())) {
            throw new ServiceException("参数校验失败");
        }
        stationCellTypeService.batchDel(dto.getIds());
        return AjaxResult.success("删除成功");
    }

}
